package com.bdqn.entity;

public class SimpleBeanConvertor {
    private static Logger logger = Logger.getLogger(String.valueOf(SimpleBeanConvertor.class));

    public static <T> T convert(HttpServletRequest req, Class<T> targetType) throws IllegalAccessException,
            InstantiationException, ParseException, UnsupportedTypeException, InvocationTargetException {
        Map<String, String[]> params = req.getParameterMap();
        if (params.size() == 0) return null;
        T target = targetType.newInstance();
        Method[] alMethods = targetType.getMethods();
        if (alMethods != null && alMethods.length > 0) {
            for (Method method : alMethods) {
                if (method.getName().startsWith("set")) {
                    Class[] args = method.getParameterTypes();
                    if (args.length == 1) {
                        String paramsName =
                                method.getName().substring(3, 4).toLowerCase(Locale.ROOT) + method.getName().substring(4);
                        if (params.containsKey(paramsName)) {
                            try {
                                Object value = ParseValue(params.get(paramsName), args[0]);
                                method.invoke(target, value);
                            } catch (ParseException e) {
                                System.out.println("参数转换错误" + e);
                                e.printStackTrace();
                                throw e;
                            }
                        } else if (Boolean.class == args[0] || boolean.class == args[0]) {
                            method.invoke(target, false);
                        }
                    }
                }
            }
        }
        return target;
    }

    private static Object ParseValue(String[] value, Class type) throws ParseException, UnsupportedTypeException {
        if (String.class == type) {
            return value[0];
        }
        if (java.util.Date.class == type) {
            return new SimpleDateFormat("yyyy-MM-dd").parse(value[0]);
        }
        if (boolean.class == type || Boolean.class == type) {
            return true;
        }
        if (char.class==type||Character.class==type){
            return value[0].charAt(0);
        }if (int.class==type||Integer.class==type){
            return Integer.valueOf(value[0]);
        }
        if (short.class==type||byte.class==type||long.class==type||float.class==type||double.class==type){

        }
        try {
            type=Class.forName("java.lang."+type.getName().substring(0,1).toUpperCase()+type.getName().substring(1));
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
        if (type.getName().startsWith("java.lang.")||type.getName().startsWith("java.math.")){

        }
        try {
            return type.getConstructor(String.class).newInstance(value[0]);

        } finally {
            return null;
        }

    }
    public class UnsupportedTypeException extends Exception{
        public UnsupportedTypeException(){
            super("不支持的类型转换");
        }
        public UnsupportedTypeException(Throwable e){
            super("不支持的类型转换",e);
        }
    }
}
